package com.dc.androidutilcode.utils;

import android.os.Handler;
import android.os.Looper;

public class ThreadUtils {

    private static final Handler HANDLER = new Handler(Looper.getMainLooper());

    /**
     * 判断当前是否主线程
     *
     * @return 当前是否主线程
     */
    public static boolean isMainThread() {
        return Looper.myLooper() == Looper.getMainLooper();
    }

    /**
     * 获取主线程Handler
     *
     * @return 主线程Handler
     */
    public static Handler getMainHandler() {
        return HANDLER;
    }

    public static void runOnUiThread(final Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        } else {
            HANDLER.post(runnable);
        }
    }
}
